package com.example.userconsumer.service;

import com.example.userconsumer.controller.NotifyToUserWebSocket;
import com.example.userconsumer.controller.SseController;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息订阅者类
 * @author likaiping
 * @createTime 2024/7/7 14:47
 */
@Service
public class RedisMessageSubscriber {
/*    @Resource
    private WebSocketSender webSocketSender;*/
/*    @Resource
    private SseController sseController;*/
    @Resource
    private NotifyToUserWebSocket notifyToUserWebSocket;
    public void onMessage(String message, String channel) {
        System.out.println("Received message: " + message + " from channel: " + channel);
        System.out.println("接收消息: " + message + " 来自频道: " + channel);
        // 处理接收到的秒杀商品ID
        handleSeckillMessage(message);
    }

    private void handleSeckillMessage(String message) {
        // 在这里处理接收到的秒杀商品ID
        System.out.println("Handling seckill for goods ID: " + message);
        // 例如，更新库存，通知用户等
        // 通知用户秒杀活动开始
//        webSocketSender.sendSeckillNotification("Seckill started for goods ID: " + message);
        notifyToUserWebSocket.sendAllMessage("Seckill started for goods ID: " + message);
        //SSE Seckill started for goods ID: 发送秒杀商品的id给前端用户
//        sseController.sendNotification(message);
    }
}
